// ==UserScript==
// @name         多平台店铺名提取
// @namespace    http://tampermonkey.net/
// @version      1.6
// @author       楚子风
// @description  支持淘宝（含天猫）、拼多多（pinduoduo.com & yangkeduo.com）、抖音、京东四个平台，提取店铺名并提供复制功能。
// @match        *://*.taobao.com/*
// @match        *://*.tmall.com/*
// @match        *://*.pinduoduo.com/*
// @match        *://*.yangkeduo.com/*
// @match        *://*.jinritemai.com*
// @match        *://*.jd.com/*
// @grant        GM_setClipboard
// @grant        GM_addStyle
// @run-at       document-end
// @updateURL    https://raw.gitcode.com/qq_43582824/1/raw/main/%E5%BA%97%E9%93%BA%E5%90%8D%E6%8F%92%E4%BB%B6-%E4%BA%AC%E4%B8%9C.js
// @downloadURL  https://raw.gitcode.com/qq_43582824/1/raw/main/%E5%BA%97%E9%93%BA%E5%90%8D%E6%8F%92%E4%BB%B6-%E4%BA%AC%E4%B8%9C.js
// ==/UserScript==


(function () {
    'use strict';

    let 按钮已创建 = false;

    function 显示软弹窗(按钮, 内容) {
        let 弹窗 = document.createElement('div');
        弹窗.textContent = 内容;
        弹窗.style.position = 'absolute';
        弹窗.style.left = '50%';
        弹窗.style.transform = 'translateX(-50%)';
        弹窗.style.bottom = '110%';
        弹窗.style.background = 'rgba(0, 0, 0, 0.85)';
        弹窗.style.color = '#fff';
        弹窗.style.padding = '6px 10px';
        弹窗.style.borderRadius = '5px';
        弹窗.style.fontSize = '12px';
        弹窗.style.whiteSpace = 'nowrap';
        弹窗.style.transition = 'opacity 0.4s ease-in-out';
        弹窗.style.opacity = '1';
        弹窗.style.pointerEvents = 'none';

        按钮.appendChild(弹窗);

        setTimeout(() => {
            弹窗.style.opacity = '0';
            setTimeout(() => 弹窗.remove(), 400);
        }, 1500);
    }

    function 显示复制按钮(店铺名) {
        if (!店铺名 || 按钮已创建) return;
        按钮已创建 = true;

        let 按钮 = document.createElement('div');
        按钮.textContent = `📋 ${店铺名}`;
        按钮.style.position = 'fixed';
        按钮.style.left = '10px';
        按钮.style.top = '100px';
        按钮.style.background = '#fff';
        按钮.style.border = '2px solid #90ee90';
        按钮.style.boxShadow = '0 4px 10px rgba(0,0,0,0.15)';
        按钮.style.padding = '5px 10px';
        按钮.style.borderRadius = '6px';
        按钮.style.cursor = 'pointer';
        按钮.style.zIndex = '9999';
        按钮.style.fontSize = '14px';
        按钮.style.textAlign = 'center';

        按钮.addEventListener('click', function () {
            GM_setClipboard(店铺名);
            显示软弹窗(按钮, '✅ 复制成功');
        });

        document.body.appendChild(按钮);
    }

    function 获取店铺名() {
        try {
            let 域名 = window.location.hostname;
            let 店铺名 = null;

            if (域名.includes("taobao.com") || 域名.includes("tmall.com")) {
                let 店铺名元素 = document.querySelector('[class^="shopName-"]');
                店铺名 = 店铺名元素 ? 店铺名元素.textContent.trim() : null;

            } else if (域名.includes("pinduoduo.com") || 域名.includes("yangkeduo.com")) {
                let 店铺名XPath = "(//span[.='进店逛逛']/parent::div/parent::div/preceding-sibling::*[1]//*[string-length(normalize-space(text())) > 0])[1]";
                let 店铺名元素 = document.evaluate(店铺名XPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                店铺名 = 店铺名元素 ? 店铺名元素.textContent.trim() : null;

            } else if (域名.includes("jinritemai.com")) {
                let 店铺名XPath = "//div[text()='进店逛逛']/preceding-sibling::*[1]/div/div[contains(@class, 'shop-content__basic-info__title-area__name')]";
                let 店铺名元素 = document.evaluate(店铺名XPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                店铺名 = 店铺名元素 ? 店铺名元素.textContent.trim() : null;

            } else if (域名.includes("jd.com")) {
                // **第一种京东 XPath**
                let 店铺名XPath1 = "//*[@class='shop-information']//*[@class='name-tag']//*[@class='name']";
                let 店铺名元素1 = document.evaluate(店铺名XPath1, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                
                if (店铺名元素1) {
                    return 店铺名元素1.textContent.trim();
                }

                // **第二种京东 XPath**
                let 店铺名XPath2 = "//*[@class='name']//*[@href[starts-with(., '//mall.jd.com')]]";
                let 店铺名元素2 = document.evaluate(店铺名XPath2, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                
                return 店铺名元素2 ? 店铺名元素2.textContent.trim() : null;
            }

            return 店铺名;
        } catch (e) {
            console.error("店铺名获取失败:", e);
            return null;
        }
    }


    // 监听 DOM 变化，避免页面重载时丢失按钮
    let 观察者 = new MutationObserver(() => {
        let 店铺名 = 获取店铺名();
        if (店铺名) {
            显示复制按钮(店铺名);
            观察者.disconnect();
        }
    });

    观察者.observe(document.body, { childList: true, subtree: true });

    // 初始运行
    setTimeout(() => {
        let 店铺名 = 获取店铺名();
        if (店铺名) {
            显示复制按钮(店铺名);
        }
    }, 2000);
})();

